package low;

import java.util.Arrays;

/**
 *977. 有序数组的平方
 */
public class SortedSquares {
    public static void main(String[] args) {
        int[] nums = new int[]{-4,-1,0,3,10};
        int[] arrays = sortedSquares(nums);
        System.out.println(Arrays.toString(arrays));
    }

    public static int[] sortedSquares(int[] nums) {
        int n = nums.length;
        int[] arrys = new int[n];
        for(int i=0, j=n-1, pos=n-1;i<=j;){
            if (nums[i]*nums[i]>nums[j]*nums[j]){
                arrys[pos] = nums[i]*nums[i];
                ++i;
            }else{
                arrys[pos] = nums[j]*nums[j];
                --j;
            }
            --pos;
        }
        return arrys;
    }
}
